package com.juyu.miliao.authentication.dao.redis;

import java.util.Optional;
import java.util.Set;

import com.juyu.miliao.common.constant.RedisKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;


@Repository
public class UserRedisDao {
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	public Optional<Integer> getUserIdByUserName(String username){
		Object o = stringRedisTemplate.opsForHash().get("username", username);
		Integer userId = null;
		if (o != null) {
			userId = Integer.parseInt(o.toString());
		}
		return Optional.ofNullable(userId);
	}
	public void addUserName(String username, long userId){
		stringRedisTemplate.opsForHash().put("username", username, userId + "");
	}
	
	public void addUserIdAndOpenId(long userId, String openId){
		stringRedisTemplate.opsForHash().put("openId", openId, userId + "");
	}
	
	public Optional<Integer> getUserIdByOpenId(String openId){
		Integer userId = (Integer) stringRedisTemplate.opsForHash().get("openId", openId);
		return Optional.ofNullable(userId);
	}

	public Boolean getUserIsEnableByDeviceNo(String deviceNo) {
		return	stringRedisTemplate.opsForSet().isMember(RedisKeys.USER_LOGIN_DIVICE_NO.getValue(),deviceNo);
	}

	/**
	 * 新注册三天内用户
	 * @param userId
	 */
    public void addThreeDaysNewUser(Long userId) {
		stringRedisTemplate.opsForZSet().add("threeDaysNewUser",userId+"",System.currentTimeMillis());
    }

    public void addNewUserBindingPhoneGiftDeposit(long userId){
    	stringRedisTemplate.opsForSet().add("newUserBindingPhoneGiftDeposit",userId+"");
	}


    /**
	 * @param nickName
	 * @return
	 */
//	public Optional<Integer> getUserIdByNickName(String nickName) {
//		Object o = stringRedisTemplate.opsForHash().get("nickname", nickName);
//		Integer userId = null;
//		if (o != null) {
//			userId = Integer.parseInt(o.toString());
//		}
//		return Optional.ofNullable(userId);
//	}

	/**
	 * @param userId
	 * @param nickname
	 */
//	public void addUserIdAndNickName(int userId, String nickname) {
//		stringRedisTemplate.opsForHash().put("nickname", nickname, userId+"");
//	}

	//1.0机器人功能
//	public void addUserMessage(long userId) {
//		String oneTime = LocalDateTime.now().plus(1,ChronoUnit.MINUTES).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//		stringRedisTemplate.opsForHash().put("userMessageScheduledOne", userId+"", oneTime);
//
//		String fifteenTime = LocalDateTime.now().plus(15,ChronoUnit.MINUTES).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//		stringRedisTemplate.opsForHash().put("userMessageScheduledFifteen", userId+"", fifteenTime);
//	}

	//1.1新机器人功能
	//线上登录用户 1分，2分，5分发送视频
//	public void userLoginMessage(long userId){
//		JSONObject json = new JSONObject();
//		json.put("type",4);
//		json.put("packageId",1);
//		json.put("userId",userId);
//		//1分发送
//		long timestamp = LocalDateTime.now().plus(60, ChronoUnit.SECONDS).toInstant(ZoneOffset.of("+8")).toEpochMilli();
//		stringRedisTemplate.opsForHash().put("robotMessageScheduled", NumberTool.randomInstant(timestamp), json.toString());
//		//2分发送
//		timestamp = LocalDateTime.now().plus(60*2, ChronoUnit.SECONDS).toInstant(ZoneOffset.of("+8")).toEpochMilli();
//		stringRedisTemplate.opsForHash().put("robotMessageScheduled", NumberTool.randomInstant(timestamp), json.toString());
//		//5分钟发送
//		timestamp = LocalDateTime.now().plus(60*5, ChronoUnit.SECONDS).toInstant(ZoneOffset.of("+8")).toEpochMilli();
//		stringRedisTemplate.opsForHash().put("robotMessageScheduled", NumberTool.randomInstant(timestamp), json.toString());
//	}
	//线上注册用户
	//type4视频：1分，2分，5分
	//type1文字：0分(2个),30秒，1分
	//type2免费图：1.30分
	//type3付费图：0分
//	public void userRegisterMessage(long userId){
//		JSONObject json = new JSONObject();
//		json.put("userId",userId);
//		json.put("packageId",1);
//		//文字内容：0分(2个),30秒，1分
//		json.put("type",1);
//		//0分1
//		long timestamp = LocalDateTime.now().plus(0, ChronoUnit.SECONDS).toInstant(ZoneOffset.of("+8")).toEpochMilli();
//		stringRedisTemplate.opsForHash().put("robotMessageScheduled", NumberTool.randomInstant(timestamp), json.toString());
//		//0分2
//		timestamp = LocalDateTime.now().plus(0, ChronoUnit.SECONDS).toInstant(ZoneOffset.of("+8")).toEpochMilli();
//		stringRedisTemplate.opsForHash().put("robotMessageScheduled", NumberTool.randomInstant(timestamp), json.toString());
//		//30秒
//		timestamp = LocalDateTime.now().plus(30, ChronoUnit.SECONDS).toInstant(ZoneOffset.of("+8")).toEpochMilli();
//		stringRedisTemplate.opsForHash().put("robotMessageScheduled", NumberTool.randomInstant(timestamp), json.toString());
//		//1分钟
//		timestamp = LocalDateTime.now().plus(60, ChronoUnit.SECONDS).toInstant(ZoneOffset.of("+8")).toEpochMilli();
//		stringRedisTemplate.opsForHash().put("robotMessageScheduled", NumberTool.randomInstant(timestamp), json.toString());
//		//免费图：1.30分
//		json.put("type",2);
//		timestamp = LocalDateTime.now().plus(60+30, ChronoUnit.SECONDS).toInstant(ZoneOffset.of("+8")).toEpochMilli();
//		stringRedisTemplate.opsForHash().put("robotMessageScheduled", NumberTool.randomInstant(timestamp), json.toString());
//		//付费图：0分
//		json.put("type",3);
//		timestamp = LocalDateTime.now().plus(0, ChronoUnit.SECONDS).toInstant(ZoneOffset.of("+8")).toEpochMilli();
//		stringRedisTemplate.opsForHash().put("robotMessageScheduled", NumberTool.randomInstant(timestamp), json.toString());
//		//视频内容：1分，2分，5分
//		json.put("type",4);
//		//1分钟
//		timestamp = LocalDateTime.now().plus(60, ChronoUnit.SECONDS).toInstant(ZoneOffset.of("+8")).toEpochMilli();
//		stringRedisTemplate.opsForHash().put("robotMessageScheduled", NumberTool.randomInstant(timestamp), json.toString());
//		//2分钟
//		timestamp = LocalDateTime.now().plus(60*2, ChronoUnit.SECONDS).toInstant(ZoneOffset.of("+8")).toEpochMilli();
//		stringRedisTemplate.opsForHash().put("robotMessageScheduled", NumberTool.randomInstant(timestamp), json.toString());
//		//5分钟
//		timestamp = LocalDateTime.now().plus(60*5, ChronoUnit.SECONDS).toInstant(ZoneOffset.of("+8")).toEpochMilli();
//		stringRedisTemplate.opsForHash().put("robotMessageScheduled", NumberTool.randomInstant(timestamp), json.toString());
//	}

	/**
	 * 线下注册用户机器人消息
	 * @param userId
	 */
//	public void vestRegisterMessage(long userId,int isOnline){
//		JSONObject json = new JSONObject();
//		json.put("userId",userId);
//		json.put("isOnline",isOnline);
//		json.put("actionId",0);
//		json.put("sendId",0);
//		long timestamp = LocalDateTime.now().plus(10, ChronoUnit.SECONDS).toInstant(ZoneOffset.of("+8")).toEpochMilli();
//		stringRedisTemplate.opsForHash().put("vestRobotMessageScheduled", NumberTool.randomInstant(timestamp), json.toString());
//	}

}
